﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

/// <summary>
///AjaxHelper 的摘要说明
/// </summary>
public class AjaxHelper
{
    /// <summary>
    /// 获取指定Page对象中标记为Ajax的方法集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="page"></param>
    /// <returns></returns>
    public static Dictionary<string, string> GetAjaxMethods<T>()
    {
        Dictionary<string, string> ajaxMethods = new Dictionary<string, string>();
        AjaxMethodAttribute ajaxMethodAttribute = null;
        //方法别名
        string methodAliasName = string.Empty;

        Type t = typeof(T);
        MethodInfo[] methods = t.GetMethods();
        foreach (MethodInfo method in methods)
        {
            object[] attributes = method.GetCustomAttributes(typeof(AjaxMethodAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                ajaxMethodAttribute = (AjaxMethodAttribute)attributes[0];
                //如果没有为ajax调用方法设置别名则用方法本身的名称
                methodAliasName = string.IsNullOrEmpty(ajaxMethodAttribute.MethodAliasName) == true ? method.Name : ajaxMethodAttribute.MethodAliasName;
                ajaxMethods.Add(method.Name, methodAliasName);
            }
        }
        return ajaxMethods;
    }
}